<?php
namespace Common\Model;
use Think\Model;
	
class FreeAgreementModel extends Model{
	public $pagesize = 10;

	/**
	 * 合同详情
	 * @param  [type] $agreement_id 合同ID
	 */
	public function info($agreement_id){
		$where['id']        = $agreement_id;
		$where['status']    = 1;
		$where['is_delete'] = 0;
		$info = $this->where($where)->find();
		$info = $this->filter_field($info);
		return $info;
	}
	public function get_list($where = '', $order = '', $page = 1, $pagesize = 0){
		$page      = $page ? $page : 1;
		$pagesize  = $pagesize ? $pagesize : $this->pagesize;
		$first_row = $pagesize * ($page - 1);

		//默认排序
		if(!$order){
			$order['order_id']  = 'asc';
			$order['edit_time'] = 'desc';
			$order['id']        = 'desc';
		}

		$where['status']    = 1;
		$where['is_delete'] = 0;
		$count = $this->where($where)->count();
		$list  = $this->field('content', true)->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			$new_list[] = $this->filter_field($rs);
		}
		$this->count = $count;
		return $new_list ? $new_list : array();
	}

	public function filter_field($data){
		$data['agreement_id']  = $data['id'];
		$data['date_desc']     = date('Y-m-d', $data['edit_time']);
		$data['time_desc']     = time_ago_desc($data['edit_time']);
		$data['datetime_desc'] = date('Y-m-d H:i:s', $data['edit_time']);
		$data['images_url']    = D('File')->get_path($data['images_id']);
		$data['category_name'] = D('Category')->get_name($data['category_id']);

		unset($data['id']);
		return $data;
	}
	/**
	 * 获取步骤信息
	 * @param  [type]  $agreement_id 合同ID
	 * @param  integer $step         步骤
	 */
	public function get_step_info($agreement_id, $step = 1){
		$ask_info  = D('FreeAgreementAsk')->where(array('agreement_id' => $agreement_id))->find();
		$ask_data  = $ask_info['ask_data'] ? unserialize($ask_info['ask_data']) : array();
		$step_data = $ask_info['step_data'] ? unserialize($ask_info['step_data']) : array();

		$data['ask_data']   = array();
		$data['step_data']  = array();
		$data['step_num']   = 0;
		$data['progress']   = 0;
		$data['step_title'] = '';
		if($ask_info){
			$step_num = $ask_info['step_num'];
			$progress = $step == 1 ? 0 : intval(($step / $step_num) * 100);
			$progress = $progress == 100 ? $progress - 6 : $progress;
			$data['ask_data']   = $step ? $ask_data[$step -1] : $ask_data;
			$data['step_data']  = $step ? $step_data[$step -1] : $step_data;
			$data['step_num']   = $step_num;
			$data['step_title'] = $data['step_data']['title'];
			$data['progress']   = $progress;
		}

		return $data;
	}
	/**
	 * 获取合同文档（替换后）
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $agreement_id 合同ID
	 */
	public function get_doc_content($user_id, $agreement_id, $order_sn = ''){
		$where['id'] = $agreement_id;
		$content = $this->where($where)->getField('content');

		preg_match_all("/{(.*?)}/", $content, $label_data);

		if($order_sn){
			$answer = D('Order')->get_ask_answer($order_sn);
		}else{
			$answer = $this->get_ask_answer($user_id, $agreement_id);
		}
		$new_content = $content;
		foreach ($label_data[1] as $label) {
			$search      = '{' . $label . '}';
			$key         = md5(strip_tags($label));
			$replace     = $answer[$key];
			if(is_array($replace)){
				if($replace[0] == 'ask-group-info'){
					$name_info = explode(',', $replace[1]);
					$i = 0;
					unset($replace[0], $replace[1]);
					$html = '';
					foreach ($replace as $val) {
						$html .= '<p>' . $name_info[$i] . '：' . $val . '</p>';
						$i++;
					}
					$replace = $html;
				}
				elseif($replace['type'] == 'radio-text'){
					$replace = $replace[$replace['val']]['text'];
				}else{
					$replace = implode(',', $replace);
				}
			}
			$replace     = is_array($replace) ? implode(',', $replace) : nl2br($replace);
			$new_content = str_replace($search, $replace, $new_content);
		}
		$new_content = preg_replace("/ style=\".*?\"/si", "", $new_content);
		
		return $new_content;
	}
	/**
	 * 保存问答
	 * @param [type] $user_id      用户ID
	 * @param [type] $agreement_id 合同ID
	 * @param [type] $data         答案数据
	 */
	public function set_ask_answer($user_id, $agreement_id, $data){
		$user_id = $user_id ? $user_id : session_id();
		$data    = $data ? $data : array();
		// 获取保存数据
		$cache = $this->get_ask_answer($user_id, $agreement_id);
		// 合并数据
		$answer_data = array_merge($cache, $data);
		// 暂存数据
		S('free-agreement-answer-' . $user_id . '-' . $agreement_id, $answer_data);
		return $answer_data;
	}
	/**
	 * 获取问答数据
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $agreement_id 合同ID
	 */
	public function get_ask_answer($user_id, $agreement_id){
		$session_id = session_id();
		// 获取保存数据
		$tmp_data  = S('free-agreement-answer-' . $session_id . '-' . $agreement_id);
		$user_data = S('free-agreement-answer-' . $user_id . '-' . $agreement_id);
		$data = $user_data ? $user_data : $tmp_data;

		return $data ? $data : array();
	}
	/**
	 * 清空问答数据
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $agreement_id 合同ID
	 */
	public function clear_ask_answer($user_id, $agreement_id){
		$session_id = session_id();
		S('free-agreement-answer-' . $session_id . '-' . $agreement_id, null);
		S('free-agreement-answer-' . $user_id . '-' . $agreement_id, null);

		return true;
	}
	/**
	 * 获取合同标签
	 * @param  [type] $agreement_id 合同ID
	 */
	public function get_label_list($agreement_id){
		$label = array();
		$ask_info  = D('FreeAgreementAsk')->where(array('agreement_id' => $agreement_id))->find();
		$ask_data  = $ask_info['ask_data'] ? unserialize($ask_info['ask_data']) : array();
		foreach ($ask_data as $list) {
			foreach ($list as $rs) {
				$label[] = $rs['name'];
			}
		}

		return $label;
	}
	
}




